package com.ostrich.widgetdemo;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class YaoLikeView extends RelativeLayout {
    //图片集合
    private List<Integer> mList = new ArrayList<>();
    //设置图片资源
    public void setImages(Integer... images){
        mList.addAll(Arrays.asList(images));
    }
    //展示画面
    public void addFavor(){
        //1：创建ImageView对象并添加到当前容器
        createImageView();

    }

    private void createImageView() {
        ImageView imageView = new ImageView(getContext());
        LayoutParams layoutParams = new LayoutParams(200, 200);
        imageView.setLayoutParams(layoutParams);
        int position = (int) (Math.random()*mList.size());//随机图片
        imageView.setImageResource(mList.get(position));
        addView(imageView);

        startAnimator(imageView);

    }

    private void startAnimator(ImageView imageView){
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        //确定4个点
        Point p0 = new Point(width/2,height);//起点
        Point p1 = new Point((int)(Math.random()*width),height*2/3);
        Point p2 = new Point((int)(Math.random()*width),height*1/3);
        Point p3 = new Point(width/2,0);//终点
        //开启动画：贝塞尔曲线
        ValueAnimator valueAnimator = ValueAnimator.ofObject(new YaoEvaluator(p1,p2),p0,p3);
        valueAnimator.setDuration(2000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Point  point = (Point) animation.getAnimatedValue();
                imageView.setX(point.x);
                imageView.setY(point.y);
                //修改透明度
                float fraction = animation.getAnimatedFraction();//0-1
                imageView.setAlpha( 1 -fraction );


            }
        });
        valueAnimator.start();
        //动画结束移除imaageview
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                removeView(imageView);
            }
        });



    }
    //估值器
    class YaoEvaluator implements TypeEvaluator<Point> {
        private Point p1;//途经点1
        private Point p2;//途经点2

        public YaoEvaluator(Point p1, Point p2) {
            this.p1 = p1;
            this.p2 = p2;
        }
        //p0:起点  p3：终点  t：0-1
        @Override
        public Point evaluate(float t, Point p0, Point p3) {

            Point point = new Point();
            point.x = (int) (p0.x * (1-t) * (1-t) * (1-t)
                                +3 * p1.x * t * (1-t) * (1-t)
                                +3 * p2.x * t * t * (1-t)
                                +p3.x * t * t * t);

            point.y = (int) (p0.y * (1-t) * (1-t) * (1-t)
                    +3 * p1.y * t * (1-t) * (1-t)
                    +3 * p2.y * t * t * (1-t)
                    +p3.y * t * t * t);

            return point;
        }
    }




    public YaoLikeView(Context context) {
        super(context);
    }

    public YaoLikeView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public YaoLikeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public YaoLikeView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
}
